package com.wframe.project.former.web.products;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.frame.pageform.DataPageForm;
import com.frame.pageform.annotation.Login;
import com.wframe.project.former.dataservices.products.ProductClassService;

/**
 * 
 * 功能:商品类目管理<br>
 * 参数:<br>
 * 作者:王海锋<br>
 * 时间:2017-1-6上午12:32:07<br>
 */
@Login
public class ProductClassPage extends DataPageForm{

	private JdataBean bean;
	private String rootCode;
	
	private ProductClassService classService=ProductClassService.getIntance();
	
	public String loadClassList() throws Exception{
		if(bean==null)bean=new JdataBean();
		String pid="";
		if(StringUtils.isNotBlank(bean.getString("code"))){
			JdataBean pbean=classService.getClassByCode(bean.getString("code"));
			pid=pbean.getString("id");
		}
		List<JdataBean> list=classService.loadClassList(bean.getString("code"));
		for(JdataBean data:list){
			if(data.getString("code").split("\\.").length==2)data.put("state", "closed");
			if(StringUtils.isNotBlank(bean.getString("code")) && data.getString("pid").equals(pid))data.put("_parentid", 0);
		}
		getJsonObject().put("total",list.size());
    	getJsonObject().put("rows", list);
    	return getJsonObject().toString().replaceAll("_parentid", "_parentId");
	}
	
	public String loadClassTree() throws Exception{
		if(bean==null)bean=new JdataBean();
		JdataBean pbean=classService.getClassByCode(bean.getString("code"));
		if(pbean==null)pbean=new JdataBean();
		List<JdataBean> list=classService.loadClassTree(pbean.getInt("id"));
		return getJsonObject().JSONDATA+getJsonObject().objToJson(list);
	}
	
	public String loadClassTree2() throws Exception{
		if(bean==null)bean=new JdataBean();
		JdataBean pbean=classService.getClassByCode(bean.getString("code"));
		if(pbean==null)pbean=new JdataBean();
		List<JdataBean> list=classService.loadClassTree(pbean.getInt("id"));
		JdataBean node=new JdataBean();
		node.put("id", "null");
		node.put("text","未分类");
		list.add(0,node);
		return getJsonObject().JSONDATA+getJsonObject().objToJson(list);
	}
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public String show() throws Exception {
		if(StringUtils.isNotBlank(bean.getString("id")))
			bean=classService.getBean(bean.getString("id"));
		else{
			if(StringUtils.isNotBlank(bean.getString("parcode"))){
				JdataBean pbean=classService.getClassByCode(bean.getString("parcode"));
				bean.put("pid", pbean.getString("id"));
			}else bean.put("pid", 1);
		}
		return "/webadmin/product/productClassEdit.jsp";
	}

	@Override
	public String toDel() throws Exception {
		classService.deleteClass(bean.getString("id"));
		return getJsonObject().toString();
	}

	@Override
	public boolean toUpdate() throws Exception {
		boolean creatcode=false;
		if(StringUtils.isBlank(bean.getString("pid"))){
			JdataBean getbean=classService.getClassByCode(rootCode);
			bean.put("pid", getbean.getString("id"));
		}
		if(StringUtils.isNotBlank(bean.getString("id"))){
			JdataBean getbean=classService.getBean(bean.getString("id"));
			if(!getbean.getString("pid").equals(bean.getString("pid")))
				creatcode=true;
		}else creatcode=true;
		if(creatcode){
			JdataBean classbean=classService.getBean(bean.getString("pid"));
			String code=classService.createClassCode(classbean.getString("code"));
			bean.put("code", code);
		}
		if(StringUtils.isNotBlank(bean.getString("id"))){
			Map key=new HashMap();
			key.put("id", bean.getString("id"));
			classService.update(bean.getDataMap(), key);
		}else classService.save(bean.getDataMap());
		getJsonObject().getJsonPage().closeWin("ok");
		return false;
	}
	/**
	 * 
	 * 功能:进入商品模型管理页<br>
	 * 参数:<br>
	 * 作者:王海锋<br>
	 * 时间:2017-1-7下午03:33:38<br>
	 * @throws Exception 
	 */
	public String showModelAttr() throws Exception{
		JdataBean cbwean=classService.getBean(bean.getString("classid"));
		bean.put("classcode", cbwean.getString("code"));
		getRequest().setAttribute("attrs", classService.queryModelAttr(cbwean.getString("code"),2));
		getRequest().setAttribute("saleattrs", classService.queryModelAttr(cbwean.getString("code"),3));
		return "/webadmin/product/modelAttrs.jsp";
	}

	public String showModelAttrAdd() throws Exception{
		if(bean!=null && StringUtils.isNotBlank(bean.getString("id"))){
			bean=classService.getModelAttr(bean.getString("id"));
		}
		return "/webadmin/product/modelAttrsEdit.jsp";
	}
	public String saveModelAttr() throws Exception{
		String id=classService.saveModelAttr(bean);
		JdataBean redata=new JdataBean();
		redata.put("state", "ok");
		redata.put("id", id);
		redata.put("name", bean.getString("name"));
		getJsonObject().getJsonPage().closeWin(getJsonObject().objToJson(redata));
		return getJsonObject().toString();
	}
	public String delModelAttr() throws Exception{
		classService.delModelAttr(bean.getString("id"));
		return getJsonObject().toString();
	}
	
	public String showModelAttrItemAdd() throws Exception{
		if(bean!=null && StringUtils.isNotBlank(bean.getString("id"))){
			bean=classService.getModelAttrItem(bean.getString("id"));
		}
		return "/webadmin/product/modelAttrsItemEdit.jsp";
	}
	public String saveModelAttrItem() throws Exception{
		String id=classService.saveModelAttrItem(bean);
		JdataBean redata=new JdataBean();
		redata.put("state", "ok");
		redata.put("id", id);
		redata.put("text", bean.getString("text"));
		getJsonObject().getJsonPage().closeWin(getJsonObject().objToJson(redata));
		return getJsonObject().toString();
	}
	public String loadModelAttrItem() throws Exception{
		getJsonObject().put("items", classService.queryModelAttrItem(bean.getString("attrid")));
		return getJsonObject().toString();
	}
	public String delModelAttrItem() throws Exception{
		if(StringUtils.isNotBlank(bean.getString("ids"))){
			String[] ids=bean.getString("ids").split(",");
			for(String id:ids){
				classService.delModelAttrItem(id);
			}
		}
		return getJsonObject().toString();
	}
	
	/**
	 * 二级栏目主推商品显示
	 * @return
	 * @throws Exception 
	 */
	public String featuredlist() throws Exception{
		if(bean==null)bean=new JdataBean();
		setPagination(classService.queryFeatured(bean.getDefault("code","").toString(), getPageSize_(), getPageNum()));
		return "/webadmin/product/featuredList.jsp";
	}
	
	public String featuredShow() throws Exception{
		if(bean==null)bean=new JdataBean();
		setPagination(classService.queryFeatured(bean.getString(""), getPageSize_(), getPageNum()));
		return "/webadmin/product/featuredAdd.jsp";
	}
	public String loadFeaturedProducts() throws Exception{
		List<JdataBean> list=classService.getFeaturedProducts(bean.getString("code"));
		getJsonObject().put("total",list.size());
    	getJsonObject().put("rows", list);
    	return getJsonObject().toString();
	}
	public String featuredSave() throws Exception{
		String[] proids=null;
		if(StringUtils.isNotBlank(bean.getString("proids")))proids=bean.getString("proids").split(",");
		bean.remove("proids");
		classService.featuredSave(bean.getString("code"), proids);
		getJsonObject().put("state", "ok");
		return getJsonObject().toString();
	}
	public String featuredDel() throws Exception{
		if(getPrimaryKey_()!=null){
			for(String id:getPrimaryKey_()){
				classService.featuredDel(id);
			}
		}
		return getJsonObject().toString();
	}
	
	public JdataBean getBean() {
		return bean;
	}

	public void setBean(JdataBean bean) {
		this.bean = bean;
	}

	public String getRootCode() {
		return rootCode;
	}

	public void setRootCode(String rootCode) {
		this.rootCode = rootCode;
	}

}
